<template>
  <a-row type="flex" :gutter="16">
    <a-col :md="5" :sm="24">
      <summary-staff-tree v-model="nodeObj" @selectedNode="selectedNode"></summary-staff-tree>
    </a-col>
    <a-col :md="24-5" :sm="24">
      <a-card :bordered="false" >
        <a-form layout="inline" >
          <a-row :gutter="24">
            <a-col :xl="8" :lg="8" :md="8" :sm="24">
              <a-form-item label="时间">
                <g-year-picker
                  placeholder="请选择时间"
                  :is-init="false"
                  v-model="date"
                ></g-year-picker>
              </a-form-item>
            </a-col>
            <a-col :xl="16" :lg="16" :md="16" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <!--                <a-button style="margin-left: 20px"  class="Template" type="primary" icon="download" @click="handleExportXlsTemplate('科研管理')">下载模板</a-button>-->
                <a-upload v-if="nodeObj.id=='A05'" name="file" style="margin-left: 20px" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel" v-has="'user:education-service-operate'">
                  <a-button v-has="'user:operate'" type="primary" icon="import">导入</a-button>
                </a-upload>
              <!--              v-show="value.id != 'A05'"-->
            </span>
            </a-col>
          </a-row>
          <a-row>
            <research-kygl-list v-if="nodeObj.isLeaf" v-model="nodeObj" :date="date"></research-kygl-list>
            <summary-org-list v-else-if="nodeObj.id=='A05'" :date="date" :score-type="scoreType"></summary-org-list>
            <summary-list v-else :date="date" :score-type="scoreType" :org-code="nodeObj.id"></summary-list>
          </a-row>
        </a-form>
      </a-card>
    </a-col>
  </a-row>
</template>

<script>
import Vue from 'vue'
import { ACCESS_TOKEN, CURRENT_DATE, TENANT_ID } from '@/store/mutation-types'
import { Modal } from 'ant-design-vue'
import store from '@/store'
import GYearPicker from '@views/gfxy/common/GYearPicker'
import SummaryStaffTree from '@views/gfxy/statistics/summary/common/SummaryStaffTree'
import SummaryList from '@views/gfxy/statistics/summary/common/SummaryList'
import ResearchKyglList from '@views/gfxy/statistics/summary/research/temp/ResearchKyglList'
import SummaryOrgList from '@views/gfxy/statistics/summary/common/SummaryOrgList'
export default {
  name: "ResearchKygl",
  components: { SummaryList, GYearPicker ,SummaryStaffTree,ResearchKyglList, SummaryOrgList},
  data(){
    return {
      currentOrgCode: '',
      nodeObj:{},
      date:'',
      url:{
        importExcelUrl:"/items/kygl/importExcel",
      },
      scoreType:4
    }
  },
  created() {
    this.date = Vue.ls.get(CURRENT_DATE);
  },
  computed: {
    //token header
    tokenHeader(){
      let head = {'X-Access-Token': Vue.ls.get(ACCESS_TOKEN)}
      let tenantid = Vue.ls.get(TENANT_ID)
      if(tenantid){
        head['tenant-id'] = tenantid
      }
      return head;
    },
    importExcelUrl: function(){
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    }
  },
  methods: {
    selectedNode(nodeObj){
      console.log("selectedNode",nodeObj)
      this.nodeObj = nodeObj
      this.$nextTick(()=>{

      })
    },
    handleImportExcel(info){
      if (info.file.status !== 'uploading') {
        console.log(info.file, info.fileList);
      }

      if (info.file.status === 'done') {

        if (info.file.response.success) {
          if (info.file.response.result.code === 201) {

            let { message, result: { msg, fileUrl, fileName } } = info.file.response.result
            let href = window._CONFIG['domianURL'] + fileUrl
            this.$warning({
              title: message,
              content: (<div>
                  <span>{msg}</span><br/>
                  <span>具体详情请 <a href={href} target="_blank" download={fileName}>点击下载</a> </span>
                </div>
              )
            })
          } else {
            this.$message.success(info.file.response.result.message || `${info.file.name} 文件上传成功`)
          }
          this.loadData()
        } else {
          this.$message.error(`${info.file.name} ${info.file.response.message}.`);
        }
      } else if (info.file.status === 'error') {
        if (info.file.response.status === 500) {
          let data = info.file.response
          const token = Vue.ls.get(ACCESS_TOKEN)
          if (token && data.message.includes("Token失效")) {
            Modal.error({
              title: '登录已过期',
              content: '很抱歉，登录已过期，请重新登录',
              okText: '重新登录',
              mask: false,
              onOk: () => {
                store.dispatch('Logout').then(() => {
                  Vue.ls.remove(ACCESS_TOKEN)
                  window.location.reload();
                })
              }
            })
          }
        } else {
          this.$message.error(`文件上传失败: ${info.file.msg} `);
        }
      }
    },

  },
}
</script>

<style scoped>

</style>